package com.xexun.gpstracker.baby.activity;

import android.Manifest;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.xexun.gpstracker.channel.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Fuminghui on 2017/10/30.
 */

public class ListViewDialog extends Dialog{

    private String TAG = ListViewDialog.class.getSimpleName();

    private final Context mContext;
    private ListView mListView;
    private List<PhoneNumber> phoneList = new ArrayList<PhoneNumber>();
    public Button cancelBtn;

    private View.OnClickListener onClickListener;

    public void setOnClickListener(View.OnClickListener onClickListener){
        this.onClickListener = onClickListener;

    }


    public ListViewDialog(Context context, List<PhoneNumber>phoneList, View.OnClickListener onClickListener) throws IllegalAccessException {
        super(context);
        mContext = context;
        this.phoneList = phoneList;
        initView();
        initListView();
        setOnClickListener(onClickListener);

    }

    private void initView() {
        View contentView = View.inflate(mContext, R.layout.content_dialog, null);
        mListView = (ListView) contentView.findViewById(R.id.lv);
//        cancelBtn =  (Button) findViewById(R.id.listviewdialog_btn);
//        cancelBtn.setOnClickListener(onClickListener);
        setContentView(contentView);
    }

    private void initListView() throws IllegalAccessException {
//        ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(mContext, android.R.layout.simple_expandable_list_item_1);
//        for (int i = 0; i < 10; i++) {
//            stringArrayAdapter.add("item " + i);
//        }
//        mListView.setAdapter(stringArrayAdapter);

        CallAdapter callAdapter = new CallAdapter(mListView,mContext,phoneList);
        callAdapter.setOnCallClickListener(new CallAdapter.OnCallClickListener() {
            @Override
            public void onClick(PhoneNumber phone, int position) {

                //打电话
                Intent intent = new Intent(Intent.ACTION_CALL);
                Uri data = Uri.parse("tel:" + phone.getCallNumber());
                Log.i(TAG,"开始拨打："+phone.getCallNumber());
                intent.setData(data);
                if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
                Log.i(TAG,"正在拨打："+phone.getCallNumber());
                mContext.startActivity(intent);


            }
        });

        mListView.setAdapter(callAdapter);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if (!hasFocus) {
            return;
        }
        setHeight();
    }

    private void setHeight() {
        Window window = getWindow();
        DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
        WindowManager.LayoutParams attributes = window.getAttributes();
        if (window.getDecorView().getHeight() >= (int) (displayMetrics.heightPixels * 0.6)) {
            attributes.height = (int) (displayMetrics.heightPixels * 0.6);
        }
        window.setAttributes(attributes);
    }

}
